var oRatio = { ratio : 1 };
var oImgDims = {};

var oHistory = [];


function append_history(oObj) {
	
	if (oHistory.length === 0) {
		oHistory.push(oObj);
		return;
	}
	
	
	if ($('.edit_media_history').length === 0) {
		oHistory.push(oObj);
		return;
	}	
	
	if ($('.edit_media_history:last').hasClass('edit_media_history_active') === true) {
		oHistory.push(oObj);
		return;
	}
	
	
	$('.edit_media_history').each(
		function(nr) {
			if ($(this).hasClass('edit_media_history_active') === true) {
				oHistory.length = (nr + 1);
				
				oHistory.push(oObj);
			}
		}
	);
	
}


function publish_history(sRemoveTool) {
	sRemoveTool = sRemoveTool || 'Y';
	
	if (oHistory.length > 0) {
		$('#edit_media_history_cont').show();
		
		$('#edit_media_history').html('');
		
		$.each(oHistory, function(iKey, oVal) {
			var oDiv = document.createElement('div');
			
			$(oDiv).addClass('edit_media_history').attr('id', 'edit_media_history_' + iKey).html(oVal.sAction).appendTo($('#edit_media_history'));
		});
		
		$('.edit_media_history:last').addClass('edit_media_history_active');
		
		if (sRemoveTool === 'Y') {
			$('#edit_media_tool_cont').html('');
		
			$('.edit_media_tool_selected').removeClass('edit_media_tool_selected');
		}
	}
}


function set_zoom_level(iImgWidth, iImgHeight) {
	iImgWidth = iImgWidth || $('#edit_media_image').width();
	iImgHeight = iImgHeight || $('#edit_media_image').height();
	
	var iImgContWidth = $('#edit_media_right').width();
	var iImgContHeight = $('#edit_media_right').height();
	
	var iZoom = 100;
	
	oImgDims.width = iImgWidth;
	oImgDims.height = iImgHeight;
	
	
	if ((iImgWidth > iImgContWidth) || (iImgHeight > iImgContHeight)) {
		
		oRatio.width = iImgWidth / iImgContWidth;
		oRatio.height = iImgHeight / iImgContHeight;
		oRatio.ratio = 1;
		
		if (oRatio.width > oRatio.height) {
			oImgDims.width = Math.floor(iImgWidth / oRatio.width);
			oImgDims.height = Math.floor(iImgHeight / oRatio.width);
			oRatio.ratio = oRatio.width;
		} else {
			oImgDims.width = Math.floor(iImgWidth / oRatio.height);
			oImgDims.height = Math.floor(iImgHeight / oRatio.height);
			oRatio.ratio = oRatio.height;
		}
		
		
		iZoom = Math.floor((oImgDims.width / iImgWidth) * 100);
	}
		
	$('#edit_media_image_zoom').html('(' + iZoom + '%)');
	
	
	$('#edit_media_image').css({
			width : oImgDims.width + 'px',
			height : oImgDims.height + 'px'
		});
	
	
}



function strip_domain(sSrc) {
	sSrc = sSrc.replace(location.protocol + '//', '');
	sSrc = sSrc.replace(location.host, '');
	return sSrc;		
}


function remove_iframe() {
	if ($(top.window.document.body).children('#media_edit_media_iframe').length === 1) {
		top.window.remove_iframe();
	} else {
		parent.window.remove_iframe();
	}
}

$(document).ready(
	function() {
		
		$('.edit_media_tool').bind('click', function() {
			if ($(this).hasClass('edit_media_tool_selected') === false) {
				$('.edit_media_tool_selected').removeClass('edit_media_tool_selected');
			
				$(this).addClass('edit_media_tool_selected');
				
				var oCallback = $(this).attr('id').replace('media_tool_', '');
				
				if (oJcrop !== false) {
					oJcrop.destroy();
				}
				
				$('#edit_media_tool_cont').html('');
				
				eval(oCallback + '()');
			
			}
		});
		
		
		
		$('.edit_media_history').live('click', function(e) {
			var iHistory = parseInt($(this).attr('id').replace('edit_media_history_', ''), 10);
			
			if ($(this).hasClass('edit_media_history_active') === false) {
				
				$('.edit_media_history_active').removeClass('edit_media_history_active');
					
				$(this).addClass('edit_media_history_active');
					
				$('#edit_media_image').attr('src', oHistory[iHistory].sImgSrc).css({
					width : oHistory[iHistory].iImgWidth + 'px',
					height : oHistory[iHistory].iImgHeight + 'px'
				});
				
				set_zoom_level();
					
				
			}
			
		});
		
		
		// bind undo and redo events
		$(document).bind('keydown', 'ctrl+z', function() {
			if ($('.edit_media_history').length > 1) {
				if ($('.edit_media_history:first').hasClass('edit_media_history_active') === false) {
					$('.edit_media_history_active').prev().trigger('click');
				}
			}
		});


		$(document).bind('keydown', 'ctrl+y', function() {
			if ($('.edit_media_history').length > 1) {
				if ($('.edit_media_history:last').hasClass('edit_media_history_active') === false) {
					$('.edit_media_history_active').next().trigger('click');
				}
			}
		});
		
		
		$('#edit_media_image_close').bind('click', function() {
			if (confirm('Are you sure you wish to exit?')) {
				if (oHistory.length > 0) {
					$.post('/admin/media_ajax/remove_tmp', { iMediaId : oMedia.iMediaId }, function(data) {
						remove_iframe();
					}, 'json');
				} else {
					remove_iframe();
				}
			}
		});
		
		$('#edit_media_image_save').bind('click', function() {
			if (confirm('Are you sure you wish to save your changes?')) {
				if (oHistory.length > 0) {
					$.post('/admin/media_ajax/save_tmp', { iMediaId : oMedia.iMediaId, sSrcImage : $('#edit_media_image').attr('src') }, function(data) {
						remove_iframe();
					}, 'json');
				} else {
					remove_iframe();
				}
			}
		});
		
		
		$(parent.window.document.body).children('#media_edit_media_iframe').css('display', 'block').prev().remove();

		$('#admin_cont').css({
			width: '100%',
			paddingTop: '0px'
		});
		
		var iContWidth = $('#edit_media_cont').width();
		var iLeftWidth = $('#edit_media_left').outerWidth();
		
		var iRightWidth = iContWidth - iLeftWidth - 30;
		
		$('#edit_media_right').width(iRightWidth + 'px');
		
		var iHeaderHeight = $('#edit_media_header').height();
		var iDocHeight = $(document).height();
		
		var iContHeight = iDocHeight - iHeaderHeight;
		
		$('#edit_media_cont').height(iContHeight + 'px');
		$('#edit_media_left').height(iContHeight + 'px');
		$('#edit_media_right').height(iContHeight + 'px');
		
		$('#edit_media_image').show();
		
		
		append_history({ 
			sAction : '<?php echo addslashes($aMessages['admin_edit_media_original']); ?>',
			sImgSrc : $('#edit_media_image').attr('src'),
			iImgWidth : $('#edit_media_image').width(),
			iImgHeight : $('#edit_media_image').height()
		});
		
		
		
		set_zoom_level($('#edit_media_image').width(), $('#edit_media_image').height());

	}
);